// pages/shops/order/orde_details.js
import Toast from '/vant-weapp/toast/toast';

const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: null,
        num: 1,
        price: 0,
        integral: 0,
        iosTextareaClass: '',
        address: null,
        isPopup: false,
        isConfirm: false,
        isPopupTextarea: false,
        textareaText: '',
        coupons: null,
        successText: '支付成功'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function ({
                          id
                      }) {
        this.setData({
            id: id
        })
        this.getOrderDetail(id)
    },

    getOrderDetail(id) {
        app.order.getOrderDetail(id)
            .then(data => {
                data.order.credit_price = parseInt(data.goods.credit_price)
                this.setData({
                    ...data
                })
            })
            .catch(err => {
                app.toast(err)
            })
    },


    onStepperChange(e) {
        this._priceCombined(e.detail)
    },


    onSubmitPay(e) {
        this.setData({
            isConfirm: true
        })
    },

    getphone({detail}) {
        app.order.mobile(detail).then(data => {
            this.onSubmit()
        }).catch((err) => {
            app.toast('手机号获取失败');
        })
    },

    onPay(e) {
        this.onSubmit()
    },

    onSubmit() { // 发起微信支付
        let goods_type = this.data.goods.type
        this.onConfirmClose()
        let address_id = null;

        if (goods_type == 'deliver') {
            if (this.data.address && this.data.address.id && this.data.goods.use_type == 1) {
                address_id = this.data.address.id;
            } else {
                wx.showToast({title: '请填写收货地址'})
                return
            }
        } else {
            address_id = 0
        }

        Toast.loading({
            mask: true,
            forbidClick: true,
            duration: 0,
            message: '加载中...'
        });
        app.order.pay({
            order_id: this.data.order.id,
            total: this.data.order.total,
            address_id: address_id,
            remark: this.data.textareaText
        })
            .then(data => {
                Toast.clear() // 关闭加载提示
                if (data.pay_success && data.pay_success === true) {
                    this.setData({
                        isPopup: true,
                        successText: '提交成功'
                    })
                    return
                }
                wx.requestPayment({
                    timeStamp: data.timestamp,
                    nonceStr: data.nonceStr,
                    package: data.package,
                    signType: data.signType,
                    paySign: data.paySign,
                    success: (res) => {
                        this.setData({
                            isPopup: true,
                            successText: '支付成功'
                        })
                    },
                    fail: () => {
                        app.toast('取消支付', 'error')
                    }
                })
            })
            .catch(err => {
                Toast.clear() // 关闭加载提示
                app.toast(err, 'error');
            })
    },

    _priceCombined(num) {
        let integral = parseInt(this.data.goods.credit_price) * num
        let price = parseFloat(this.data.goods.price) * num;
        this.setData({
            'order.credit_price': integral,
            'order.total': num,
            'order.price': price.toFixed(2)
        });
    },


    onClose(event) {
        this.setData({
            isPopup: false
        });
        if (event.detail === 'confirm') {
            wx.redirectTo({
                url: '/pages/my/order/index',
            })
        } else {
            wx.switchTab({
              url: '/pages/tabpage/index',
            })
        }
    },

    onConfirmClose() {
        this.setData({
            isConfirm: false
        })
    },


    onLeaveMsg() {
        let textareaText = this.data.textareaText
        wx.navigateTo({
            url: `/pages/shops/order/leave_msg?text=${textareaText}`
        })
    },

    onPreferential() { // 打开优惠券
        wx.navigateTo({
            url: `/pages/my/coupons/index?type=0`
        })
    },

    addAddress() {
        var order_id = this.data.order.id;
        wx.navigateTo({
            url: '/pages/my/address/index?order_id=' + order_id
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var address = wx.getStorageSync('curr_address');
        if (address) {
            this.setData({
                address: address
            });
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
